package com.apestech.framework.db.queryData;

import com.apestech.framework.db.SqlResultSet;
import com.apestech.framework.db.callback.QueryCallBackHandler;
import com.apestech.framework.db.callback.ResultOutPut;
import com.apestech.framework.db.callback.TableJsonCallBackHandler;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;

/**
 * @author hup
 * @create 2018-08-16-15:46
 */
public class QueryJson implements QueryCallBackHandler {
    private JSONArray list;
    private String timeStampField;
    private TableJsonCallBackHandler tableJsonCallBackHandler;
    private String tableName;

    @Override
    public void getData(SqlResultSet rs, JSONArray rslist) throws Exception {
        if(tableJsonCallBackHandler==null) {
            list =rs.toJSON();
        } else {
            list=new JSONArray();
            while(rs.next()){
                JSONObject m=tableJsonCallBackHandler.getJSON(tableName, rs.toRowJson());
                list.add(m);
            }
        }
        if(rs.getConParam()!=null){
            if(rs.getConParam().getSize()>0){
                JSONObject param=new JSONObject();
                param.put("TotalPage", rs.getConParam().getTotalPage());
                param.put("Size", rs.getConParam().getSize());
                param.put("Total", rs.getConParam().getTotal());
                param.put("Page", rs.getConParam().getPage());
                JSONObject p=new JSONObject();
                p.put("SYS_PAGE", param);
                if(list==null) {
                    list=new JSONArray();
                }
                list.add(0, p);
            }
        }
    }

    public void setTimeStampField(String timeStampField){
        this.timeStampField=timeStampField;
    }

    public void setTableJsonCallBackHandler(String tableName, TableJsonCallBackHandler tableJsonCallBackHandler) {
        this.tableJsonCallBackHandler = tableJsonCallBackHandler;
        this.tableName=tableName;
    }

    public JSONArray getList(){
        return list;
    }
}
